<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" v-auth="'run.create'" @click="create"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" v-auth="'run.delete'" v-if="selection.length>0" :disabled="selection.length==0" @click="dels"></el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search" placeholder="搜索" @clear="this.$TABLE.searchSubmit(this)" clearable></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="this.$TABLE.searchSubmit(this)"></el-button>
                    <imiFilterBar :options="options" @filterChange="filterChange"></imiFilterBar>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" :params="params" @selection-change="selectionChange" stripe remoteSort remoteFilter>
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column label="运营中心ID" prop="id" >
                                    
				</el-table-column>
                <el-table-column label="运营中心名" prop="name" >
                                    
				</el-table-column>
                <el-table-column label="后台ID" prop="admin_id" >
                                    
				</el-table-column>
                <el-table-column label="手机号" prop="mobile" >
                                    
				</el-table-column>
                <el-table-column label="创建时间" prop="createtime"  align="right" width="180" :formatter="this.$TABLE.datetime">
                                    
				</el-table-column>
                <el-table-column label="更新时间" prop="updatetime"  align="right" width="180" :formatter="this.$TABLE.datetime">
                                    
				</el-table-column>
                <el-table-column label="密码" prop="password" >
                                    
				</el-table-column>
                <el-table-column label="用户ID" prop="user_id" >
                    <template #default="scope"  >
                    <el-tag >{{user_id_option[scope.row.user_id]}} </el-tag>
                    </template>  
				</el-table-column>
                <el-table-column label="积分" prop="score" >
                                    
				</el-table-column>

<!-- 
                <el-table-column label="消费总额占比%" prop="consume_total_ratio" >
                                    
                </el-table-column>
                <el-table-column label="营业总额占比%" prop="trade_total_ratio" >
                                    
                </el-table-column>
                <el-table-column label="消费增长率1" prop="consume_inc_ratio1" >
                                    
                </el-table-column>
                <el-table-column label="发放占比%" prop="consume_send_ratio1" >
                                    
                </el-table-column>
                <el-table-column label="消费增长率2" prop="consume_inc_ratio2" >
                                    
                </el-table-column>
                <el-table-column label="发放占比%" prop="consume_send_ratio2" >
                                    
                </el-table-column>
                <el-table-column label="消费增长率3" prop="consume_inc_ratio3" >
                                    
                </el-table-column>
                <el-table-column label="发放占比%" prop="consume_send_ratio3" >
                                    
                </el-table-column>
                <el-table-column label="消费增长率4" prop="consume_inc_ratio4" >
                                    
                </el-table-column>
                <el-table-column label="发放占比%" prop="consume_send_ratio4" >
                                    
                </el-table-column>
                <el-table-column label="营业增长率1" prop="trade_inc_ratio1" >
                                    
                </el-table-column>
                <el-table-column label="发放占比%" prop="trade_send_ratio1" >
                                    
                </el-table-column>
                <el-table-column label="营业增长率2" prop="trade_inc_ratio2" >
                                    
                </el-table-column>
                <el-table-column label="发放占比%" prop="trade_send_ratio2" >
                                    
                </el-table-column>
                <el-table-column label="营业增长率3" prop="trade_inc_ratio3" >
                                    
                </el-table-column>
                <el-table-column label="发放占比%" prop="trade_send_ratio3" >
                                    
                </el-table-column>
                <el-table-column label="营业增长率4" prop="trade_inc_ratio4" >
                                    
                </el-table-column>
                <el-table-column label="发放占比%" prop="trade_send_ratio4" >
                                    
                </el-table-column>
                <el-table-column label="推广额度阈值M" prop="extend_threshold" >
                                    
                </el-table-column>
                <el-table-column label="上月激励金发放比例" prop="last_month_excite_send_ratio" >
                                    
                </el-table-column> -->
                
                <el-table-column label="操作" fixed="right" align="right" width="140">
                    <template #default="scope">
                        <el-button type="text" v-auth="'order.update'" size="small" @click="find_runset(scope.row)">查看设置比例
                        </el-button>
                        <el-button type="text" v-auth="'run.update'" size="small" @click="update(scope.row)">编辑
                        </el-button>
                        <el-popconfirm title="确定删除吗？" @confirm="del(scope.row)">
                            <template #reference>
                                <el-button type="text" v-auth="'run.delete'" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>
<script>
    import saveDialog from './save';
    import imiFilterBar from '@/components/imiFilterBar';
    export default {
        name: 'run',
        components: {
            saveDialog,
            imiFilterBar
        },
        data() {
            return {
                options: [
                    {
                        label: '运营中心ID',
                        value: 'id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: 'name',
                        value: 'name',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '后台ID',
                        value: 'admin_id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '手机号',
                        value: 'mobile',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '创建时间',
                        value: 'createtime',
                        type: 'datetimerange',
                        operator: 'between',
                    },
					{
                        label: '更新时间',
                        value: 'updatetime',
                        type: 'datetimerange',
                        operator: 'between',
                    },
					{
                        label: '密码',
                        value: 'password',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '用户ID',
                        value: 'user_id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '积分',
                        value: 'score',
                        type: 'text',
                        operator: '=',
                    },
					                    
                ],
                dialog: {
                    save: false
                },
                apiObj: this.$API.run.read,
                selection: [],
                search: '',
                params:{},
                user_id_option:[],
            }
        },
        created() {
            this.$TABLE.setFilterValue(this);
            this.getUser();
        },
        mounted() {
            
        },
        methods: {
            async getUser(){
                var res = await this.$API.fa_user.read.get({page: 1,pageSize: 999999})
                var list = res.data.list;
                var cate = [];
                for (let index = 0; index < list.length; index++) {
                    cate[list[index].id] = list[index].id+'👉'+list[index].nickname;                    
                }
                console.log(cate);
                this.user_id_option = cate;
            },

                        // 点击查看子订单
            find_runset(row){
              let id = JSON.parse(JSON.stringify(row)).id
              this.$router.push({
                path: "/run_commission_set/index",
                query: {
                  run_id: id
                },
              });
            },

            filterChange(data) {
                this.$TABLE.filter(data, this);
            },
            create() {
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open()
                })
            },
            update(row) {
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open('update').setData(row)
                })
            },
            async del(row) {
                var res = await this.$API.run.delete.post({ids: row.id});
                if (res.code == 200) {
                    this.$refs.table.refresh()
                    this.$message.success(res.message)
                } else {
                    this.$alert(res.message, "提示", {type: 'error'})
                }
            },
            async dels() {
                if (!this.selection.length) {
                    this.$message.warning("请选择要删除的数据")
                    return false;
                }
                var ids = [];
                for (const i in this.selection) {
                    ids.push(this.selection[i].id)
                }
                this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                    type: 'warning'
                }).then(() => {
                    const loading = this.$loading();
                    this.$API.run.delete.post({ids:ids}).then(res => {
                        if (res.code == 200) {
                            this.$refs.table.refresh()
                            this.$message.success(res.message)
                        } else {
                            this.$message.error(res.message)
                        }
                        loading.close();
                    });
                })
            },
            selectionChange(selection) {
                this.selection = selection;
            },
            handleSuccess() {
                this.$refs.table.refresh()
            }
        }
    }
</script>